package leetcode_301_400;

import java.net.DatagramPacket;
import java.security.acl.AclNotFoundException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class LeeCode_318 {
    public static void main(String[] args) {
        System.out.println(maxProduct2(new String[]{"abc", "daef"}));
    }
    private static int maxProduct2(String[] words) {
        int n = words.length;
        int[] ws = new int[n];
        for (int i = 0; i < n; i++) {
            int num = 0;
            for (int j = 0; j < words[i].length(); j++) {
                num |= 1 << (words[i].charAt(j) - 'a');
            }
            ws[i] = num;
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if ((ws[i] & ws[j]) == 0)
                    ans = Math.max(ans, words[i].length() * words[j].length());
            }
        }
        return ans;
    }
    private static int maxProduct(String[] words) {
        Set<Integer>[] sets = new HashSet[26];
        for (int i = 0; i < sets.length; i++) {
            sets[i] = new HashSet<>();
        }
        int n = words.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < words[i].length(); j++) {
                sets[words[i].charAt(j) - 'a'].add(i);
            }
        }
        Set<Integer> set = new HashSet<>();
        for (Set<Integer> son : sets) {
            for (Integer s1 : son) {
                for (Integer s2 : son) {
                    int key = s1 * n + s2;
                    set.add(key);
                }
            }
        }
        int max = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (!set.contains(i * n + j)){
                    max = Math.max(max, words[i].length() * words[j].length());
                }
            }
        }
        return max;
    }
}
